package com.example.aircraftticketingsystemserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.aircraftticketingsystemserver.entity.Flight;
import com.example.aircraftticketingsystemserver.entity.Passenger;
import com.example.aircraftticketingsystemserver.mapper.PassengerMapper;
import com.example.aircraftticketingsystemserver.service.IPassengerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.aircraftticketingsystemserver.utils.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxz
 * @since 2024-08-19
 */
@Service
@Transactional
public class PassengerServiceImpl extends ServiceImpl<PassengerMapper, Passenger> implements IPassengerService {
    @Resource
    private PassengerMapper passengerMapper;
    @Override
    public void addPassenger(Passenger passenger) {
        passengerMapper.addPassenger(passenger);

    }

    @Override
    public void addPassengerAndLinkUser(Passenger passenger, Integer userId) {
        if(getPassengerByNumber(passenger.getPassengerNumber())==null){
        passengerMapper.addPassenger(passenger);
        Integer passengerId = passenger.getPassengerId();
        passengerMapper.addPassengerAndLinkUser(userId, passengerId);}
        else {
            Integer passengerId=getPassengerByNumber(passenger.getPassengerNumber()).getPassengerId();
            passengerMapper.addPassengerAndLinkUser(userId, passengerId);}
        }


    @Override
    public void deletePassengerById(Integer passengerId) {
        passengerMapper.deletePassengerById(passengerId);
    }

    @Override
    public void batchDeletePassenger(List<Integer> passengersId) {
        passengerMapper.batchDeletePassenger(passengersId);
    }

    @Override
    public void updatePassenger(Passenger passenger) {
        passengerMapper.updatePassenger(passenger);
    }

    @Override
    public Page getPassengersByParams(Integer pageNo, Integer pageSize,String passengerName, String passengerPhone, String passengerNumber) {
        Page page=new Page();
        PageHelper.startPage(pageNo,pageSize);
        List<Passenger> passengers=passengerMapper.getPassengersByParams(passengerName,passengerPhone,passengerNumber);
        PageInfo<Passenger> info=new PageInfo<>(passengers);
        page.setPage(info.getList());
        page.setPageNo(info.getPageNum());
        page.setPageSize(info.getPageSize());
        page.setHasNext(info.isHasNextPage());
        page.setHasPre(info.isHasPreviousPage());
        page.setTotal(info.getTotal());
        page.setPageCount(info.getPages());
        return page;
    }


    public Passenger getPassengerByNumber(String passengerNumber) {

        QueryWrapper<Passenger> passengerQueryWrapper = new QueryWrapper<>();

        passengerQueryWrapper.eq("passenger_number", passengerNumber);
        return baseMapper.selectOne(passengerQueryWrapper);
    }
}
